﻿using System;
using System.Collections.Generic;
using System.Text;

namespace UtilZ.Dotnet.Ex.Model
{
    /// <summary>
    /// 选中项基类
    /// </summary>
    public class CheckItemBase : CheckItemShowAbs, ICheckItem
    {
        private bool _checked = false;
        /// <summary>
        /// 是否选中[true:选中;false:未选中]
        /// </summary>
        public bool IsChecked
        {
            get { return _checked; }
            set
            {
                if (_checked == value)
                {
                    return;
                }

                if (this.OnPreCheckedChanged(this._checked, value))
                {
                    this.SetIsChecked(value, true);
                }
            }
        }

        /// <summary>
        /// 设置是否选中值
        /// </summary>
        /// <param name="isChecked">是否选中[true:选中;false:未选中]</param>
        /// <param name="raiseNotifyPropertyChanged">是否触发属性改变事件[true:触发;false:不触发]</param>
        public void SetIsChecked(bool isChecked, bool raiseNotifyPropertyChanged)
        {
            bool oldChecked = this._checked;
            this._checked = isChecked;
            base.OnRaisePropertyChanged(nameof(IsChecked));

            if (raiseNotifyPropertyChanged)
            {
                this.CheckedChanged?.Invoke(this, new CheckedArgs<ICheckItem>(this, oldChecked, isChecked));
            }
        }



        /// <summary>
        /// 是否选中改变事件
        /// </summary>
        public event EventHandler<CheckedArgs<ICheckItem>> CheckedChanged;





        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="isChecked">是否选中[true:选中;false:未选中]</param>
        public CheckItemBase(bool isChecked = false)
            : this(null, isChecked)
        {

        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="text">显示文本</param>
        /// <param name="isChecked">是否选中[true:选中;false:未选中]</param>
        public CheckItemBase(string text, bool isChecked = false)
            : base(text)
        {
            this._checked = isChecked;
        }



        /// <summary>
        /// 选中项改变前处理
        /// 返回值:是否更新选中状态[true:更新;false:不更新]
        /// </summary>
        /// <param name="oldChecked">旧的的选中状态</param>
        /// <param name="newChecked">新的的选中状态</param>
        /// <returns>是否更新选中状态[true:更新;false:不更新]</returns>
        protected virtual bool OnPreCheckedChanged(bool oldChecked, bool newChecked)
        {
            return true;
        }
    }
}
